﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;
namespace QLHocSinh
{
    public partial class frmNhapDiem : Form
    {
        public frmNhapDiem()
        {
            InitializeComponent();
        }

        private int diemmieng = 0;
        private int diem15 = 0;
        private int diem1t = 0;
        private int diemthi = 0;
        private int tonghs = 0;
        //Column3
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[3].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[4].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[5].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[6].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[7].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[8].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[9].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[10].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[11].DisplayIndex || this.dgv_diem.CurrentCellAddress.X == dgv_diem.Columns[12].DisplayIndex)
            {
                TextBox txt = e.Control as TextBox;
                txt.Name = "Column4";
                if (txt != null)
                {
                    txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress);
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                    txt.Validating -= new CancelEventHandler(txt_Validating);
                    txt.Validating += new CancelEventHandler(txt_Validating);
                }
            }
        }

        ////
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Kim tra 
            TextBox txt = sender as TextBox;
            if (txt.Name == "Column4")
            {
                 //Là các ký tự số hoặc điều khiển
                if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar) || e.KeyChar == '.')
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }
        ////
        private void txt_Validating(object sender, CancelEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (txt.Name == "Column4")
            {
                try
                {
                     int i=0;
                    // int.TryParse(txt.Text, i);
                    // int.TryParse(txt.Text, i);
                }
                catch {
                    MessageBox.Show("Bạn phải nhập vào dữ liệu kiểu số.");
                }
            }
        }

        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgv_diem.Rows[e.RowIndex].Cells["Column1"].Value = e.RowIndex + 1;
        }

        private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.ColumnIndex == 12)
            {
                float dm1, dm2, dm3, l1, l2, l3, tl1, tl2, tl3, hk;
                float kq = 0;
                if (dgv_diem.Rows[e.RowIndex].Cells[3].Value.ToString() == "")
                {
                    dm1 = -1;
                }
                else
                {
                   
                    dm1 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[3].Value.ToString());
                    tonghs += diemmieng;
                    kq += (float)(diemmieng * dm1);
                }
                if (dgv_diem.Rows[e.RowIndex].Cells[4].Value.ToString() == "")
                {
                    dm2 = -1;
                }
                else
                {
                    dm2 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[4].Value.ToString());
                    tonghs += diemmieng;
                    kq += (float)(diemmieng * dm2);
                }
                if (dgv_diem.Rows[e.RowIndex].Cells[5].Value.ToString() == "")
                {
                    dm3 = -1;
                }
                else
                {
                    dm3 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[5].Value.ToString());
                    tonghs += diemmieng;
                    kq += (float)(diemmieng * dm3);
                }

                if (dgv_diem.Rows[e.RowIndex].Cells[6].Value.ToString() == "")
                {
                    l1 = -1;
                }
                else
                {
                    l1 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[6].Value.ToString());
                    tonghs += diem15;
                    kq += (float)(diem15 * l1);
                }
                if (dgv_diem.Rows[e.RowIndex].Cells[7].Value.ToString() == "")
                {
                    l2 = -1;
                }
                else
                {
                    l2 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[7].Value.ToString());
                    tonghs += diem15;
                    kq += (float)(diem15 * l2);
                }
                if (dgv_diem.Rows[e.RowIndex].Cells[8].Value.ToString() == "")
                {
                    l3 = -1;
                }
                else
                {
                    l3 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[8].Value.ToString());
                    tonghs += diem15;
                    kq += (float)(diem15 * l3);
                }

                if (dgv_diem.Rows[e.RowIndex].Cells[9].Value.ToString() == "")
                {
                    tl1 = -1;
                }
                else
                {
                    tl1 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[9].Value.ToString());
                    tonghs += diem1t;
                    kq += (float)(diem1t * tl1);
                }
                if (dgv_diem.Rows[e.RowIndex].Cells[10].Value.ToString() == "")
                {
                    tl2 = -1;
                }
                else
                {
                    tl2 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[10].Value.ToString());
                    tonghs += diem1t;
                    kq += (float)(diem1t * tl2);
                }
                if (dgv_diem.Rows[e.RowIndex].Cells[11].Value.ToString() == "")
                {
                    tl3 = -1;
                }
                else
                {
                    tl3 = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[11].Value.ToString());
                    tonghs += diem1t;
                    kq += (float)(diem1t * tl3);
                }
                if (dgv_diem.Rows[e.RowIndex].Cells[12].Value.ToString() == "")
                {
                    hk = -1;
                }
                else
                {
                    hk = float.Parse(dgv_diem.Rows[e.RowIndex].Cells[12].Value.ToString());
                    tonghs += diemthi;
                    kq += (float)(diemthi * hk);
                }
             double dtb = (double)kq / tonghs;
             dtb = Math.Round(dtb, 2);
                dgv_diem.Rows[e.RowIndex].Cells[13].Value = dtb.ToString();
            }
        }
        private void load_nienhoc()
        {
            for (int i = 2000; i < 2020; i++)
            {
               // String namhoc = i + " - " + (i + 1);
                cbb_nienhoc.Items.Add(i);
            }
            int namht = DateTime.Now.Year;
           // String nam = namht + " - " + (namht + 1);
            cbb_nienhoc.Text = namht.ToString() ;
        }

        private void load_hocky()
        {
         
            string sSQL = "Select id_hk, ten_hk from hoc_ky";
            DataTable dt = DAO.KetNoi.clsCacHamChung.select_dulieu(sSQL);
            cbb_hocky.DataSource = dt;
            cbb_hocky.DisplayMember = "ten_hk";
            cbb_hocky.ValueMember = "id_hk";
        }

        private void load_khoi()
        {
          
            string sSQL = "Select id_khoi, ten_khoi  from khoi_lop";
            DataTable dt = DAO.KetNoi.clsCacHamChung.select_dulieu(sSQL);
            cbb_khoi.DataSource = dt;
            cbb_khoi.DisplayMember = "ten_khoi";
            cbb_khoi.ValueMember = "id_khoi";
        }

        private void load_lop()
        {
            string sSQL = "Select id_lop, ten_lop  from Lop where nam_hoc = "+ cbb_nienhoc.Text +" and id_khoi = " + cbb_khoi.SelectedValue ;
            DataTable dt = DAO.KetNoi.clsCacHamChung.select_dulieu(sSQL);
            cbb_lop.DataSource = dt;
            cbb_lop.DisplayMember = "ten_lop";
            cbb_lop.ValueMember = "id_lop";
        }

        private void load_mon()
        {
            string sSQL = "Select mh.id_mh as id_mh, mh.ten_mh as ten_mh ";
            sSQL += " from mon_hoc mh, khoi_mon km where mh.id_mh = km.id_mh and km.id_khoi = " + cbb_khoi.SelectedValue;
            DataTable dt = DAO.KetNoi.clsCacHamChung.select_dulieu(sSQL);
            cbb_mon.DataSource = dt;
            cbb_mon.DisplayMember = "ten_mh";
            cbb_mon.ValueMember = "id_mh";
        }

        private void load_heso()
        {
            try
            {
                string sSQL = "Select hsd_mieng, hsd_15_phut, hsd_1_tiet, hsd_thi from mon_hoc where id_mh = " + cbb_mon.SelectedValue;
                DataTable dt = DAO.KetNoi.clsCacHamChung.select_dulieu(sSQL);
                if (dt.Rows.Count > 0)
                {
                    diemmieng = int.Parse(dt.Rows[0]["hsd_mieng"].ToString());
                    diem15 = int.Parse(dt.Rows[0]["hsd_15_phut"].ToString());
                    diem1t = int.Parse(dt.Rows[0]["hsd_1_tiet"].ToString());
                    diemthi = int.Parse(dt.Rows[0]["hsd_thi"].ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi load hệ số điểm " + ex, "Error");
            }
        }

        private void frmNhapDiem_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                load_nienhoc();
                load_hocky();
               load_khoi();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Lỗi load dữ liệu " + ex, "Error");
            }
            Cursor.Current = Cursors.Default;
        }

        private void cbb_lop_SelectedValueChanged(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                load_mon();
                // cbb_mon.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi load dữ liệu môn học " + ex, "Error");
            }

            Cursor.Current = Cursors.Default;
        }

        private void load_hocsinh_theomon()
        {
            String sSQL = "";
            sSQL += "select hs.id_hs as [Mã Học sinh], hs.ho_hs + ' ' + hs.ten_hs as [Tên học sinh],dm.d_mieng_1 as DM1, dm.d_mieng_2 as DM2, dm.d_mieng_3 as Dm3, dm.d_15_phut_1 as D15L1, dm.d_15_phut_2 as D15L2, dm.d_15_phut_3 as D15L3, dm.d_1_tiet_1 as D1L1, dm.d_1_tiet_2 as D1L2, dm.d_1_tiet_3 as D1L3, dm.d_thi as DHK, dm.dtb as TBM";
            sSQL += " from hoc_sinh hs, diem_mon dm ";
            sSQL += " where hs.id_hs = dm.id_hs and dm.id_lop = "+ cbb_lop.SelectedValue  +" and dm.id_mh = "+ cbb_mon.SelectedValue +" and dm.id_hk = " + cbb_hocky.SelectedValue ;
            DataTable dt = new DataTable();
            dt = DAO.KetNoi.clsCacHamChung.select_dulieu(sSQL);
            if (dt.Rows.Count <= 0)
            {
                sSQL = " Select hs.id_hs as [Mã Học sinh], hs.ho_hs + ' ' + hs.ten_hs as [Tên học sinh], '' as DM1, '' as DM2, '' as DM3, '' as D15L1, '' as D15L2, '' as D15L3, '' as D1L1, '' as D1L2, '' as D1L3, '' as DHK, '' as TBM  ";
                sSQL += " from hoc_sinh hs, hs_lop l where hs.id_hs = l.id_hs and hs.id_khoi = " + cbb_khoi.SelectedValue + " and l.id_lop = " + cbb_lop.SelectedValue;
                dt = DAO.KetNoi.clsCacHamChung.select_dulieu(sSQL);
            }

            dgv_diem.DataSource = dt;
            
        }

        private void cbb_mon_SelectedValueChanged(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                load_hocsinh_theomon();
                load_heso();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi load dữ liệu học sinh " + ex, "Error");
            }
            
            Cursor.Current = Cursors.Default;
        }

        private void cbb_khoi_SelectedValueChanged(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                load_lop();
                // cbb_lop.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi load dữ liệu lớp " + ex, "Error");
            }
            Cursor.Current = Cursors.Default;
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgv_diem_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           // MessageBox.Show(e.ColumnIndex.ToString());
        }

        private void btn_ghi_Click(object sender, EventArgs e)
        {
            try
            {
            
           // object []mang = new object[dgv_diem.Rows.Count + 1];
           DiemMonDAO.delete(int.Parse(cbb_hocky.SelectedValue.ToString()), int.Parse(cbb_lop.SelectedValue.ToString()), int.Parse(cbb_mon.SelectedValue.ToString()));
            //mang[0] = sSQL;
            for (int i = 0; i < dgv_diem.Rows.Count; i++)
            {
                DiemMonDTO d = new DiemMonDTO();
                d.Id_hk = int.Parse(cbb_hocky.SelectedValue.ToString());
                d.Id_lop = int.Parse(cbb_lop.SelectedValue.ToString());
                d.Id_mh = int.Parse(cbb_mon.SelectedValue.ToString());
                d.Id_hs = int.Parse(dgv_diem.Rows[i].Cells[1].Value.ToString());
               
                if (dgv_diem.Rows[i].Cells[3].Value.ToString() == "")
                {
                    d.D_mieng_1 = 0;
                }
                else
                {

                    d.D_mieng_1 = float.Parse(dgv_diem.Rows[i].Cells[3].Value.ToString());
                }
                if (dgv_diem.Rows[i].Cells[4].Value.ToString() == "")
                {
                    d.D_mieng_2 = 0;
                }
                else
                {
                    d.D_mieng_2 = float.Parse(dgv_diem.Rows[i].Cells[4].Value.ToString());
                
                }
                if (dgv_diem.Rows[i].Cells[5].Value.ToString() == "")
                {
                    d.D_mieng_3 = 0;
                }
                else
                {
                    d.D_mieng_3 = float.Parse(dgv_diem.Rows[i].Cells[5].Value.ToString());
                }

                if (dgv_diem.Rows[i].Cells[6].Value.ToString() == "")
                {
                    d.D_15_phut_1 = 0;
                }
                else
                {
                    d.D_15_phut_1 = float.Parse(dgv_diem.Rows[i].Cells[6].Value.ToString());
                }
                if (dgv_diem.Rows[i].Cells[7].Value.ToString() == "")
                {
                    d.D_15_phut_2 = 0;
                }
                else
                {
                    d.D_15_phut_2 = float.Parse(dgv_diem.Rows[i].Cells[7].Value.ToString());
                }
                if (dgv_diem.Rows[i].Cells[8].Value.ToString() == "")
                {
                    d.D_15_phut_3 = 0;
                }
                else
                {
                    d.D_15_phut_3 = float.Parse(dgv_diem.Rows[i].Cells[8].Value.ToString());
                }

                if (dgv_diem.Rows[i].Cells[9].Value.ToString() == "")
                {
                    d.D_1_tiet_1 = 0;
                }
                else
                {
                    d.D_1_tiet_1 = float.Parse(dgv_diem.Rows[i].Cells[9].Value.ToString());
                }
                if (dgv_diem.Rows[i].Cells[10].Value.ToString() == "")
                {
                    d.D_1_tiet_2 = 0;
                }
                else
                {
                    d.D_1_tiet_2 = float.Parse(dgv_diem.Rows[i].Cells[10].Value.ToString());
                }
                if (dgv_diem.Rows[i].Cells[11].Value.ToString() == "")
                {
                    d.D_1_tiet_3 = 0;
                }
                else
                {
                    d.D_1_tiet_3 = float.Parse(dgv_diem.Rows[i].Cells[11].Value.ToString());
                }
                if (dgv_diem.Rows[i].Cells[12].Value.ToString() == "")
                {
                    d.D_thi = 0;
                }
                else
                {
                    d.D_thi = float.Parse(dgv_diem.Rows[i].Cells[12].Value.ToString());
                }
                d.Dtb = float.Parse(dgv_diem.Rows[i].Cells[13].Value.ToString());
                DiemMonDAO.Insert(d);
               // mang[i + 1] = sSQL;
            }
           //bool kq = DAO.KetNoi.clsConnect.executeNonQuery(mang);
           //if (kq)
           //{
            MessageBox.Show("Thực hiện ghi dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            cbb_mon.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           //}
           //else
           //{
           //    MessageBox.Show("Thực hiện ghi dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
           //    cbb_mon.Focus();
           //}
        }

        private void frmNhapDiem_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.ActiveControl.Name != "dataGridView")
                {
                    this.SelectNextControl(this.ActiveControl, true, true, true, false);
                }
            }

            else if (e.KeyCode == Keys.Escape)
            {
                btn_thoat_Click(sender, null);
            }
        }

       
        ////
    }
}
